package com.ruicar.afs.cloud.workflow.api.sync;

import com.alibaba.fastjson.JSONObject;
import com.ruicar.afs.cloud.common.datasync.api.IParamSync;
import com.ruicar.afs.cloud.common.datasync.define.Key;
import com.ruicar.afs.cloud.common.datasync.exception.ParamSyncException;
import com.ruicar.afs.cloud.workflow.api.cachebean.FlowMappingInfo;
import com.ruicar.afs.cloud.workflow.api.constant.FlowConstant;
import com.ruicar.afs.cloud.workflow.api.util.FlowMappingHelper;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;

import java.util.List;

@Slf4j
@Component
public class FLowMappingSync implements IParamSync {
    @Override
    public Key getKey() {
        return FlowConstant.FLOW_MAPPING_SYNC_KEY;
    }

    @Override
    public void onSync(JSONObject params) throws ParamSyncException {
        log.info("流程映射配置变化接更新数据");
        if (params != null && params.getJSONArray(FlowConstant.FLOW_MAPPING_SYNC_KEY.getKey()) != null) {
            List<FlowMappingInfo> mappingInfoList = params.getJSONArray(FlowConstant.FLOW_MAPPING_SYNC_KEY.getKey()).toJavaList(FlowMappingInfo.class);
            FlowMappingHelper.setConfigMapping(mappingInfoList);
        }
    }
}
